﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using Jayden.Dll.Core.Collections;

namespace Jayden.Console.FileIndexer
{
	class Program
	{

		static List<string> Entries = new List<string>();

		static void IndexDirectory(string directory)
		{
			Entries.Add(directory);

			try
			{

				string[] files = Directory.GetFiles(directory);
				foreach (string file in files)
					Entries.Add(file);

				string[] directories = Directory.GetDirectories(directory);

				foreach (string subDirectory in directories)
					IndexDirectory(subDirectory);

			}
			catch (Exception e)
			{

			}
		}

		static BitArray[] IndexByRegex(params Regex[] regex)
		{
			BitArray[] array = new BitArray[regex.Length];
			for (int c = 0; c < array.Length; c++)
				array[c] = new BitArray(Entries.Count);

			for (int index = 0; index < Entries.Count; index++)
			{
				for (int r = 0; r < array.Length; r++)
				{
					if (regex[r].IsMatch(Entries[index]))
						array[r][index] = true;
				}
			}

			return array;
		}

		static void Main(string[] args)
		{
			string directory = @"C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\IExtractor\IExtractor\bin\Debug";
			IndexDirectory(directory);
			BitArray bitArray = new BitArray(Entries.Count);
			int emptyCount = bitArray.NumberOfBitsOn;
			bitArray.Not();

			BitArray[] extensions = IndexByRegex(new Regex(@"^.*\.(exe|EXE)$"), new Regex(@"^.*\.(png|PNG)$"), new Regex(@"^.*\.(jpg|JPG|jpeg|JPEG)$"), new Regex(@"^.*\.(gif|GIF)$"));

			BitArray others = new BitArray(Entries.Count);

			others.Or(extensions[0]);
			others.Or(extensions[1]);
			others.Or(extensions[2]);
			others.Or(extensions[3]);
			others.Not();

			System.Console.WriteLine("{0} files.", bitArray.NumberOfBits);
			System.Console.WriteLine("{0} files selected.", bitArray.NumberOfBitsOn);

			System.Console.WriteLine("{0} .exe files.", extensions[0].NumberOfBitsOn);
			System.Console.WriteLine("{0} .png files.", extensions[1].NumberOfBitsOn);
			System.Console.WriteLine("{0} .jpg files.", extensions[2].NumberOfBitsOn);
			System.Console.WriteLine("{0} .gif files.", extensions[3].NumberOfBitsOn);

			System.Console.WriteLine("{0} other files.", others.NumberOfBitsOn);

			System.Console.Write("=== press any key to exit ===");
			System.Console.ReadKey(true);
		}
	}
}
